<!DOCTYPE html>
<html>
<head>
    {include file="public/header" /}
</head>
<body>
<div class="layui-form layuimini-form">
    <div class="layuimini-main">

        <form class="layui-form" action="">

            <div class="layui-form-item">
                <label class="layui-form-label">满减名称</label>
                <div class="layui-input-block">
                    <input type="text" name="title" value="" lay-verify="title" autocomplete="off" placeholder="请输入满减名称" class="layui-input iwidth" aria-disabled="true">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">消费金额</label>
                <div class="layui-input-block">
                    <input type="number" name="price" value="" lay-verify="price" mix="1" step="0.01" autocomplete="off" placeholder="请输入消费金额" class="layui-input iwidth" aria-disabled="true">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">满减金额</label>
                <div class="layui-input-block">
                    <input type="number" name="money" value="" lay-verify="money"mix="1" step="0.01" autocomplete="off" placeholder="请输入满减金额（满消费金额减的钱数）" class="layui-input iwidth" aria-disabled="true">
                </div>
            </div>

            <div class="layui-form-item" >
                <label class="layui-form-label">发送日期</label>
                <div class="layui-input-inline">
                    <input type="text" name="start_time" id="send_time" lay-verify="send_time" value="" placeholder="yyyy-MM-dd" autocomplete="off" class="layui-input iwidth" >
                </div>

            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">下架日期</label>
                <div class="layui-input-inline">
                    <input type="text" name="end_time" id="stop_time" lay-verify="stop_time" value="" placeholder="yyyy-MM-dd" autocomplete="off" class="layui-input iwidth" >
                </div>

            </div>


            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit="" lay-filter="add">立即创建</button>
                </div>
            </div>
        </form>

    </div>
</div>

{include file="public/footer" /}

<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->
<script>
    layui.use(['form', 'layedit', 'laydate','wangEditor'], function () {
        var form = layui.form
            , layer = layui.layer
            , layedit = layui.layedit
            , laydate = layui.laydate
            , wangEditor = layui.wangEditor
            , $ = layui.jquery;
        // 监听全选
        form.on('checkbox(checkall)', function (data) {
            console.log(data);
            if (data.elem.checked) {
                $('.checkboxs').prop('checked', true);
            } else {
                $('.checkboxs').prop('checked', false);
            }
            form.render('checkbox');
        });


        $(this).removeAttr("lay-key");//时间框一点就消失处理
        // 上架开始日期
        var insShelf = laydate.render({
            elem: '#send_time'
            , min: 0
            , trigger : 'click'//时间框一点就消失处理
            , calendar: true
            , type: 'datetime'
            , done: function (value, date) {
             /*   //更新结束日期的最小日期
                insUnder.config.min = lay.extend({}, date, {
                    month: date.month - 1
                });*/

                //自动弹出结束日期的选择器
                insUnder.config.elem[0].focus();
            }
        });
        var insUnder = laydate.render({
            elem: '#stop_time'
            , min: 0
            , trigger : 'click'//时间框一点就消失处理
            , calendar: true
            , type: 'datetime'
            , done: function (value, date) {
             /*   //更新结束日期的最小日期
                insUnder.config.min = lay.extend({}, date, {
                    month: date.month - 1
                });*/

                //自动弹出结束日期的选择器
                //insUnder.config.elem[0].focus();
            }
        });
        //获取type 1 :消息通知 2 平台通知3.弹窗通知
        //用类型判断验证
        var verify_data = {
            title: function (value) {
                if (value == '') {
                    return '消息标题不能为空';
                }
            }
            ,price: function (value) {
                if (value == '') {
                    return '消费金额不能为空';
                }
            },
            money: function (value) {
                if (value == '') {
                    return '满减金额不能为空';
                }
            },
            send_time: function (value) {
                if (value == '') {
                    return '发送时间不能为空';
                }
            } ,
            stop_time: function (value) {
                if (value == '') {
                    return '结束时间不能为空';
                }
            }

        }


        //自定义验证规则
        form.verify(
            verify_data
        );

        //监听指定开关
        form.on('switch(switchTest)', function (data) {
            layer.msg('开关checked：' + (this.checked ? 'true' : 'false'), {
                offset: '6px'
            });
            layer.tips('温馨提示：请注意开关状态的文字可以随意定义，而不仅仅是ON|OFF', data.othis)
        });


        //监听提交修改
        form.on('submit(add)', function (data) {
            // var ids = [];
            // // 获取选中的id
            // $('.checkboxs').each(function (index, el) {
            //     if ($(this).prop('checked')) {
            //         ids.push($(this).val())
            //     }
            // });
            let  postdata =data.field
            // postdata.content=editor.txt.html()
            $.ajax({
                url: '/admin/reduction/create?active=ajax',
                type: 'post',
                dataType: 'json',
                data: postdata,
                success: function (res) {
                    if (res.status == true) {    // 获得frame索引
                        parent.layer.msg("操作成功!",
                            {
                                icon:1,
                                time: 1000
                            },
                            function () {
                            //重新加载父页面
                                parent.location.reload();
                        });
                        return;
                    } else{
                        layer.msg(res.msg,{ icon:5,
                            time: 1000});
                    }
                },
                error: function () {
                    layer.msg("报错了");
                }
            });
            return false;
            /*layer.alert(JSON.stringify(data.field), {
                title: '最终的提交信息'
            })*/
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            var result = JSON.stringify(data.field);
            // layer.alert(result, {
            //     title: '最终的搜索信息'
            // });
            //搜索
            let search=$("#searchuser").val();
            $.ajax({
                url: "/admin/notice/create",
                type: 'post',
                async:true,
                data:{search:search,action:'ajax'},
                dataType: 'json',
                success: function (res) {
                    if(res.status == true){
                        $(".showuser").html(res.param);
                        form.render()
                    }else{
                        layer.msg(res.msg,{icon:5,time: 1000})
                    }
                },
                error: function (res) {
                    console.log(res);
                }
            });

            return false;
        });


    });



    /*复选框全选*/
    function selectAll(checkbox) {
        $('input[type=checkbox]').prop('checked', $(checkbox).prop('checked'));
    }
</script>
</body>
</html>